﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace SizeboxUI
{
	// Token: 0x02000395 RID: 917
	public class CommandView : MonoBehaviour
	{
		// Token: 0x060017FB RID: 6139 RVA: 0x000980B0 File Offset: 0x000962B0
		private void Start()
		{
			this.mainCamera = Camera.main;
			this.control = this.mainCamera.GetComponent<InterfaceControl>();
			this.placement = this.mainCamera.GetComponent<EditPlacement>();
			this.actionButtonPrefab = Resources.Load<Button>("UI/Button/Action Button");
			this.actionPanel = base.transform.GetChild(0).gameObject;
			this.selectCommand = default(CommandAction);
			this.selectCommand.text = "Select";
			this.selectCommand.action = new Action(this.SelectTargetEntity);
			this.cancelCommand = default(CommandAction);
			this.cancelCommand.text = "Cancel";
			this.cancelCommand.action = new Action(this.HideBehaviors);
			this.playAsCommand = default(CommandAction);
			this.playAsCommand.text = "Play as";
			this.playAsCommand.action = new Action(this.PlayTargetEntity);
		}

		// Token: 0x060017FC RID: 6140 RVA: 0x000981B3 File Offset: 0x000963B3
		private void SelectTargetEntity()
		{
			this.OnSelectObject(this.targetEntity);
		}

		// Token: 0x060017FD RID: 6141 RVA: 0x000981C1 File Offset: 0x000963C1
		private void PlayTargetEntity()
		{
			this.OnPlayEntity(this.targetEntity);
		}

		// Token: 0x060017FE RID: 6142 RVA: 0x000981D0 File Offset: 0x000963D0
		private void Update()
		{
			if (!this.control.commandEnabled)
			{
				return;
			}
			if (Input.GetKeyDown(KeyCode.Escape) || Input.GetButtonDown(ButtonInput.EscapeAlt))
			{
				this.HideBehaviors();
				return;
			}
			if (Input.GetMouseButtonDown(0) && this.placement.state == EditPlacement.State.Idle)
			{
				this.startClickLeft = Time.time;
			}
			if (Input.GetMouseButtonDown(1) && this.placement.state == EditPlacement.State.Idle)
			{
				this.startClickRight = Time.time;
			}
			if (Input.GetMouseButtonDown(2) && this.placement.state == EditPlacement.State.Idle)
			{
				this.startClickCenter = Time.time;
			}
			if (Input.GetMouseButtonUp(2) && Time.time - this.startClickCenter < this.maxTimeBetweenClick && this.placement.state == EditPlacement.State.Idle)
			{
				this.targetEntity = null;
				this.SelectTargetEntity();
			}
			if (Input.GetMouseButtonUp(0) && Time.time - this.startClickLeft < this.maxTimeBetweenClick && !EventSystem.current.IsPointerOverGameObject())
			{
				if (this.showingPanel)
				{
					this.HideBehaviors();
					return;
				}
				if (this.control.selectedEntity != null && !this.control.selectedEntity.isPositioned)
				{
					return;
				}
				this.HideBehaviors();
				Ray ray = this.mainCamera.ScreenPointToRay(Input.mousePosition);
				RaycastHit raycastHit;
				if (Physics.Raycast(ray, out raycastHit, float.PositiveInfinity, Layers.actionSelectionMask, QueryTriggerInteraction.Collide))
				{
					if (raycastHit.collider.gameObject.layer == Layers.uiLayer)
					{
						return;
					}
					this.cursorPosition = raycastHit.point;
					EntityBase x = this.FindEntity(raycastHit.transform);
					if (x != null)
					{
						if (x != this.targetEntity)
						{
							this.targetEntity = x;
							this.SelectTargetEntity();
						}
						else
						{
							this.actionPanel.transform.position = Input.mousePosition;
							this.OpenCommandMenu(false);
						}
					}
				}
			}
			if (Input.GetMouseButtonUp(1) && Time.time - this.startClickRight < this.maxTimeBetweenClick && !EventSystem.current.IsPointerOverGameObject())
			{
				if (this.showingPanel)
				{
					this.HideBehaviors();
					return;
				}
				if (this.control.selectedEntity != null && !this.control.selectedEntity.isPositioned)
				{
					return;
				}
				this.HideBehaviors();
				Ray ray2 = this.mainCamera.ScreenPointToRay(Input.mousePosition);
				RaycastHit raycastHit2;
				if (Physics.Raycast(ray2, out raycastHit2, float.PositiveInfinity, Layers.actionSelectionMask, QueryTriggerInteraction.Collide))
				{
					if (raycastHit2.collider.gameObject.layer == Layers.uiLayer)
					{
						return;
					}
					this.cursorPosition = raycastHit2.point;
					this.actionPanel.transform.position = Input.mousePosition;
					this.targetEntity = this.FindEntity(raycastHit2.transform);
					if (Input.GetKey(KeyCode.LeftControl) && this.targetEntity != null)
					{
						this.SelectTargetEntity();
					}
					this.OpenCommandMenu(false);
				}
			}
		}

		// Token: 0x060017FF RID: 6143 RVA: 0x00098514 File Offset: 0x00096714
		private List<IBehavior> GetListPossibleBehaviors(EntityBase targetEntity)
		{
			if (this.control.selectedEntity == null || this.control.selectedEntity.ai == null || BehaviorLists.Instance == null)
			{
				return new List<IBehavior>();
			}
			List<IBehavior> list = BehaviorLists.Instance.FindBehaviors(this.control.selectedEntity, targetEntity);
			return list.FindAll((IBehavior beh) => !beh.IsHidden());
		}

		// Token: 0x06001800 RID: 6144 RVA: 0x0009859C File Offset: 0x0009679C
		private EntityBase FindEntity(Transform hitElement)
		{
			return hitElement.GetComponentInParent<EntityBase>();
		}

		// Token: 0x06001801 RID: 6145 RVA: 0x000985A4 File Offset: 0x000967A4
		public void SetTargetEntity(EntityBase ent)
		{
			this.targetEntity = ent;
			this.SelectTargetEntity();
		}

		// Token: 0x06001802 RID: 6146 RVA: 0x000985B4 File Offset: 0x000967B4
		public void OpenCommandMenu(bool customMenu = false)
		{
			if (!customMenu)
			{
				this.listPossibleBehaviors = this.GetListPossibleBehaviors(this.targetEntity);
				this.entries = this.LoadMenuEntries(this.listPossibleBehaviors, this.targetEntity);
				if (this.entries.menu.Count <= 0)
				{
					return;
				}
				this.ShowMenuEntries(this.entries, string.Empty);
			}
			else
			{
				this.actionPanel.transform.position = Input.mousePosition;
			}
			this.showingPanel = true;
			RectTransform component = this.actionPanel.GetComponent<RectTransform>();
			float x = component.sizeDelta.x;
			float num = (this.actionPanel.GetComponent<GridLayoutGroup>().cellSize.y + this.actionPanel.GetComponent<GridLayoutGroup>().spacing.y) * (float)this.actionPanel.transform.childCount;
			float num2 = component.position.x - x / 2f;
			float num3 = (float)Screen.height - component.position.y;
			if (num2 < 0f)
			{
				num2 = 0f;
			}
			if (num2 + x >= (float)Screen.width)
			{
				num2 = (float)Screen.width - x;
			}
			if (num < (float)Screen.height)
			{
				if (num3 < 0f)
				{
					num3 = 0f;
				}
				if (num3 + num >= (float)Screen.height)
				{
					num3 = (float)Screen.height - num;
				}
			}
			this.actionPanel.transform.position = new Vector3(num2 + x / 2f, (float)Screen.height - num3, 0f);
		}

		// Token: 0x06001803 RID: 6147 RVA: 0x00098760 File Offset: 0x00096960
		private MenuEntryList LoadMenuEntries(List<IBehavior> listBehaviors, EntityBase targetEntity)
		{
			MenuEntryList result = default(MenuEntryList);
			result.menu = new Dictionary<string, List<CommandAction>>();
			bool flag = targetEntity != this.control.selectedEntity && targetEntity != null;
			if (listBehaviors.Count == 0 && !flag)
			{
				return result;
			}
			result.menu[this.rootDir] = new List<CommandAction>();
			int count = listBehaviors.Count;
			for (int i = 0; i < count; i++)
			{
				CommandAction item = default(CommandAction);
				string[] array = listBehaviors[i].GetText().Split(new char[]
				{
					'/'
				});
				int num = array.Length;
				int j = 0;
				while (j < num)
				{
					string key;
					if (j == 0)
					{
						key = this.rootDir;
					}
					else
					{
						key = string.Join("/", array, 0, j);
					}
					item.text = array[j];
					if (j == num - 1)
					{
						int number = i;
						item.action = delegate()
						{
							this.OnBehaviorClicked(number);
						};
						goto IL_17C;
					}
					string subdir = string.Join("/", array, 0, j + 1);
					if (!result.menu.ContainsKey(subdir))
					{
						item.text += "...";
						item.action = delegate()
						{
							this.OpenSubDir(subdir);
						};
						goto IL_17C;
					}
					IL_1B7:
					j++;
					continue;
					IL_17C:
					if (!result.menu.ContainsKey(key))
					{
						result.menu[key] = new List<CommandAction>();
					}
					result.menu[key].Add(item);
					goto IL_1B7;
				}
			}
			if (flag)
			{
				result.menu[this.rootDir].Add(this.selectCommand);
			}
			if (targetEntity != null && (targetEntity.isGiantess || targetEntity.isMicro) && !targetEntity.isPlayer)
			{
				result.menu[this.rootDir].Add(this.playAsCommand);
			}
			result.menu[this.rootDir].Add(this.cancelCommand);
			return result;
		}

		// Token: 0x06001804 RID: 6148 RVA: 0x000989C9 File Offset: 0x00096BC9
		private void OpenSubDir(string subdir)
		{
			this.HideBehaviors();
			this.ShowMenuEntries(this.entries, subdir);
		}

		// Token: 0x06001805 RID: 6149 RVA: 0x000989E0 File Offset: 0x00096BE0
		private void ShowMenuEntries(MenuEntryList entryList, string dir = "")
		{
			this.showingPanel = true;
			List<CommandAction> list = entryList.menu[dir];
			foreach (CommandAction commandAction in list)
			{
				this.AddButton(commandAction);
			}
		}

		// Token: 0x06001806 RID: 6150 RVA: 0x00098A4C File Offset: 0x00096C4C
		private void OnSelectObject(EntityBase entity)
		{
			this.control.SetSelectedObject(entity, true);
			this.HideBehaviors();
		}

		// Token: 0x06001807 RID: 6151 RVA: 0x00098A64 File Offset: 0x00096C64
		private void OnPlayEntity(EntityBase entity)
		{
			this.HideBehaviors();
			ObjectManager.Instance.PlayAsEntity(entity);
			if (entity.isMicro)
			{
				base.StartCoroutine(this.SelectPlayAsMicro(GameController.playerInstance));
			}
			else
			{
				this.control.SetSelectedObject(entity, true);
			}
		}

		// Token: 0x06001808 RID: 6152 RVA: 0x00098AB4 File Offset: 0x00096CB4
		private IEnumerator SelectPlayAsMicro(EntityBase entity)
		{
			yield return new WaitForSeconds(0.01f);
			this.control.SetSelectedObject(GameController.playerInstance, true);
			yield return null;
			yield break;
		}

		// Token: 0x06001809 RID: 6153 RVA: 0x00098ACF File Offset: 0x00096CCF
		private void OnBehaviorClicked(int number)
		{
			this.TellToDoBehavior(number);
			this.HideBehaviors();
		}

		// Token: 0x0600180A RID: 6154 RVA: 0x00098AE0 File Offset: 0x00096CE0
		public void HideBehaviors()
		{
			int num = (!this.actionPanel) ? 0 : this.actionPanel.transform.childCount;
			for (int i = 0; i < num; i++)
			{
				Button component = this.actionPanel.transform.GetChild(i).gameObject.GetComponent<Button>();
				component.onClick.RemoveAllListeners();
				UnityEngine.Object.Destroy(component.gameObject);
			}
			this.showingPanel = false;
		}

		// Token: 0x0600180B RID: 6155 RVA: 0x00098B60 File Offset: 0x00096D60
		private void TellToDoBehavior(int number)
		{
			EntityBase selectedEntity = this.control.selectedEntity;
			if (selectedEntity.ai != null && selectedEntity.actionManager != null)
			{
				IBehavior behavior = this.listPossibleBehaviors[number];
				if (!behavior.IsSecondary())
				{
					selectedEntity.ai.DisableAI();
				}
				if (Input.GetKey(KeyCode.LeftShift))
				{
					selectedEntity.ai.ScheduleCommand(behavior, this.targetEntity, this.cursorPosition);
				}
				else
				{
					selectedEntity.ai.ImmediateCommand(behavior, this.targetEntity, this.cursorPosition);
				}
			}
			else
			{
				Debug.Log("No character selected");
			}
		}

		// Token: 0x0600180C RID: 6156 RVA: 0x00098C11 File Offset: 0x00096E11
		private void OnDisable()
		{
			this.HideBehaviors();
		}

		// Token: 0x0600180D RID: 6157 RVA: 0x00098C1C File Offset: 0x00096E1C
		private void AddButton(CommandAction commandAction)
		{
			Button button = UnityEngine.Object.Instantiate<Button>(this.actionButtonPrefab, this.actionPanel.transform, false);
			Text component = button.transform.GetChild(0).GetComponent<Text>();
			component.text = commandAction.text;
			button.onClick.AddListener(delegate()
			{
				commandAction.action();
			});
		}

		// Token: 0x0600180E RID: 6158 RVA: 0x00098C88 File Offset: 0x00096E88
		public void HackShowSelfBehaviors()
		{
			if (this.control.selectedEntity == null)
			{
				return;
			}
			this.targetEntity = this.control.selectedEntity;
			this.OpenCommandMenu(false);
		}

		// Token: 0x0600180F RID: 6159 RVA: 0x00098CBC File Offset: 0x00096EBC
		public Button AddCustomButton(string str)
		{
			Button button = UnityEngine.Object.Instantiate<Button>(this.actionButtonPrefab, this.actionPanel.transform, false);
			Text component = button.transform.GetChild(0).GetComponent<Text>();
			component.text = str;
			return button;
		}

		// Token: 0x04001A34 RID: 6708
		private Camera mainCamera;

		// Token: 0x04001A35 RID: 6709
		private GameObject actionPanel;

		// Token: 0x04001A36 RID: 6710
		private Button actionButtonPrefab;

		// Token: 0x04001A37 RID: 6711
		private List<IBehavior> listPossibleBehaviors;

		// Token: 0x04001A38 RID: 6712
		private InterfaceControl control;

		// Token: 0x04001A39 RID: 6713
		private Vector3 cursorPosition;

		// Token: 0x04001A3A RID: 6714
		private EntityBase targetEntity;

		// Token: 0x04001A3B RID: 6715
		private float maxTimeBetweenClick = 0.2f;

		// Token: 0x04001A3C RID: 6716
		private float startClickRight;

		// Token: 0x04001A3D RID: 6717
		private float startClickLeft;

		// Token: 0x04001A3E RID: 6718
		private float startClickCenter;

		// Token: 0x04001A3F RID: 6719
		private EditPlacement placement;

		// Token: 0x04001A40 RID: 6720
		private CommandAction selectCommand;

		// Token: 0x04001A41 RID: 6721
		private CommandAction cancelCommand;

		// Token: 0x04001A42 RID: 6722
		private CommandAction playAsCommand;

		// Token: 0x04001A43 RID: 6723
		private MenuEntryList entries;

		// Token: 0x04001A44 RID: 6724
		private string rootDir = string.Empty;

		// Token: 0x04001A45 RID: 6725
		private bool showingPanel;
	}
}
